package com.newyulong.implatform.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.LinearLayout;

import com.newyulong.implatform.IMPApplication;
import com.newyulong.implatform.R;
import com.newyulong.implatform.bean.UserInfoJsonBean;
import com.newyulong.implatform.db.UserDao;
import com.newyulong.implatform.defines.Defines;
import com.newyulong.implatform.util.CommonsUtil;


/**
 * @author qianj
 * @create 2012-8-28 下午04:26:37
 * @since
 */
public class SplashActivity extends Activity {

	SharedPreferences sp;

	private static final String TAG = "SplashActivity.class";

	private static final int DO_LOGIN = 2;

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {

			case Defines.HTTP_REQUEST_TIMEOUT:
				Intent i1 = new Intent(SplashActivity.this,LoginActivity.class);
				startActivity(i1);
				SplashActivity.this.finish();
				break;
			case DO_LOGIN:
				Intent i = new Intent(SplashActivity.this,LoginActivity.class);
				startActivity(i);
				SplashActivity.this.finish();
				break;
			default:
				break;
			}
		};
	};

	private boolean remberMe;
	private boolean netIsOk;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash);
		
		sp = IMPApplication.getInstance().sp;
		
		netIsOk = CommonsUtil.checkNetworkAvailable(this);
		remberMe = sp.getBoolean("remberMe", false);

		// 开机动画
		AlphaAnimation aa = new AlphaAnimation(0.5f, 1.0f);
		aa.setDuration(1500);
		LinearLayout rl_splash = (LinearLayout) findViewById(R.id.rl_splash);
		rl_splash.startAnimation(aa);

		aa.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
			}
			@Override
			public void onAnimationRepeat(Animation animation) {
			}
			@Override
			public void onAnimationEnd(Animation animation) {
				if (remberMe) {//记住用户
					UserDao dao = new UserDao(SplashActivity.this);
					final UserInfoJsonBean bean = dao.getUserInfo();
					if (netIsOk) {
						new Thread(new Runnable() {
							@Override
							public void run() {
								String openId = "";
								try {
									openId = IMPApplication.getInstance().userService.login(bean.getUserName(),bean.getPassword());
								} catch (Exception e) {
									handler.sendEmptyMessage(Defines.HTTP_REQUEST_TIMEOUT);
									e.printStackTrace();
								}
								IMPApplication.getInstance().setOpenId(openId);
								if (!"".equals(openId)) {
									Log.i(TAG, "Splash登录成功！");
									//跳转主界面
									Intent i = new Intent(SplashActivity.this, MainActivity.class);
									startActivity(i);
									SplashActivity.this.finish();
								} else {
									Intent i = new Intent(SplashActivity.this,LoginActivity.class);
									startActivity(i);
									SplashActivity.this.finish();
								}
							}
						}).start();
					} else {
						Intent i = new Intent(SplashActivity.this,LoginActivity.class);
						startActivity(i);
						SplashActivity.this.finish();
					}
				} else {//没有记住用户的情况
					handler.sendEmptyMessage(DO_LOGIN);
				}

			}
		});

	}

}
